import Vue from "vue";
import VueRouter from "vue-router";
import NotFoundPage from "@/components/NotFoundPage.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    components: {
      default: () => import("../views/HomeView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue")
    },
    // 需要身份验证和特定权限
    meta: { title: '首页', requiresAuth: false, isNotFound: false } // 路由元信息
  },
  {
    path: "/owner1",
    name: "owner_info",
    components: {
      default: () => import("../views/OwnerInfoView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '业主管理/业主信息管理', requiresAuth: true, permission: 1, isNotFound: false } // 路由元信息
  },
  {
    path: "/owner2",
    name: "owner_verify",
    components: {
      default: () => import("../views/OwnerVerifyView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '业主管理/业主信息审核', requiresAuth: true, permission: 1, isNotFound: false } // 路由元信息
  },
  {
    path: "/house1",
    name: "house_info",
    components: {
      default: () => import("../views/HouseInfoView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '房屋管理/房屋信息管理', requiresAuth: true, permission: 2, isNotFound: false } // 路由元信息
  },
  {
    path: "/house2",
    name: "house_verify",
    components: {
      default: () => import("../views/HouseVerifyView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '房屋管理/房屋信息审核', requiresAuth: true, permission: 2, isNotFound: false } // 路由元信息
  },
  {
    path: "/car1",
    name: "car_info",
    components: {
      default: () => import("../views/CarInfoView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '车辆管理/车辆信息管理', requiresAuth: true, permission: 3, isNotFound: false } // 路由元信息
  },
  {
    path: "/car2",
    name: "car_verify",
    components: {
      default: () => import("../views/CarVerifyView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '车辆管理/车辆信息审核', requiresAuth: true, permission: 3, isNotFound: false } // 路由元信息
  },
  {
    path: "/parking1",
    name: "parking_info",
    components: {
      default: () => import("../views/ParkingInfoView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '停车位管理/停车位信息管理', requiresAuth: true, permission: 4, isNotFound: false } // 路由元信息
  },
  // {
  //   path: "/parking2",
  //   name: "parking_verify",
  //   components: {
  //     default: () => import("../views/ParkingVerifyView.vue"),
  //     menuView: () => import("../views/MenuView.vue"),
  //     topView: () => import("../views/TopView.vue"),
  //   },
  //   meta: { title: '停车位管理/停车位信息审核', requiresAuth: true, permission: 4, isNotFound: false } // 路由元信息
  // },
  {
    path: "/gateaccess",
    name: "gate_access_info",
    components: {
      default: () => import("../views/GateAccessInfoView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '门禁管理/社区大门门禁管理', requiresAuth: true, permission: 5, isNotFound: false } // 路由元信息
  },
  {
    path: "/parkingaccess",
    name: "parking_access_info",
    components: {
      default: () => import("../views/ParkingAccessInfoView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '门禁管理/停车场门禁管理', requiresAuth: true, permission: 5, isNotFound: false } // 路由元信息
  },
  {
    path: "/sensor",
    name: "sensor_info",
    components: {
      default: () => import("../views/SensorInfoView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '门禁管理/传感器管理', requiresAuth: true, permission: 5, isNotFound: false } // 路由元信息
  },
  {
    path: "/access",
    name: "access_records",
    components: {
      default: () => import("../views/AccessRecordsView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '门禁管理/门禁使用记录管理', requiresAuth: true, permission: 5, isNotFound: false } // 路由元信息
  },
  {
    path: "/mgr",
    name: "manager",
    components: {
      default: () => import("../views/MgrView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    },
    meta: { title: '管理员管理', requiresAuth: true, permission: 0, isNotFound: false } // 路由元信息
  },
  {
    path: "/login",
    name: "login",
    components: {
      default: () => import("../views/LoginView.vue"),
      menuView: () => import("../views/MenuView.vue"),
      topView: () => import("../views/TopView.vue"),
    }
  },

  {
    path: '/404',
    name: 'NotFound',
    component: NotFoundPage,
    meta: {
      isNotFound: true, // 标记404页面不使用某些功能
    },
  },
];

const router = new VueRouter({
  routes,
});

// Token检验函数
export function checkToken() {
  // 这里实现你的Token检验逻辑
  // 例如，从localStorage获取Token，然后判断是否存在
  const token = localStorage.getItem('userToken');
  return !!token;
}

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  // 1. 检查Token
  if (!checkToken()) {
    // 如果Token无效，重定向到登录页面
    // 假设登录页面的路径是 '/login'
    if (to.path !== '/login') {
      next('/login');
    } else {
      next(); // 如果已经是在登录页面，不做任何操作
    }
  } else {
    // 如果Token有效，继续原定的路由跳转
    next();
  }

  // 你可以在这里检查to.matched.length来判断是否找到了匹配的路由
  if (to.matched.length === 0) {
    // 没有找到匹配的路由，重定向到404页面
    next('/404'); // 或者使用next({ path: '/404' });
  } else {
    next(); // 有匹配的路由，继续正常的导航
  }


// 2. 使用Vue Router的全局前置守卫来检查用户是否具有访问特定路由所需的权限（这里后端实现了）
//   const permissions = JSON.parse(localStorage.getItem('adminPermissions') || '[]');
//   const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
//
//   if (requiresAuth) {
//       const hasPermission = to.matched.some(record => {
//       return permissions.includes(record.meta.permission);
//     });
//     console.log("hasPermission", hasPermission);
//     if (hasPermission) {
//       next(); // 有权限，放行
//     } else {
//       window.alert("未拥有访问该页面的权限！");
//       next({ path: from.path }); // 权限不足，停留在当前页面
//     }
//   } else {
//     next(); // 不需要身份验证，放行
//   }
});


export default router;
